<template>
  <div class="wrapper">
    <!-- 页面头部部分 -->
    <div class="header">
      <div class="logo">
<!--        <img src="@/assets/img/logo1.jpg" alt="Logo" class="logo">-->
          后台管理系统
      </div>
      <!-- 水平一级菜单 -->
      <div style="float:left;">
        <el-menu
          mode="horizontal"
          text-color="#000000"
          active-text-color="#3989fa"
          :default-active="toIndex"
          @select="handleSelect">
          <el-menu-item v-for="(item, index) in menuList" :index="item.path" :key="item.path" v-if="!item.hidden">
            <span slot="title">{{ item.name }}</span>
          </el-menu-item>
        </el-menu>
      </div>

      <div class="header-right">
        <div class="header-user-con">
          <!-- 切换主题 -->
          <div @click="handleChangeStyle()">
            <el-tooltip content="切换主题" placement="bottom">
              <i :class="globalTheme ? 'el-icon-moon' : 'el-icon-sunny'"></i>
            </el-tooltip>
          </div>
          <!-- 用户头像 -->
          <div class="user-avator">
            <img src="@/assets/img/img.jpg"/>
          </div>
          <!-- 用户名下拉菜单 -->
          <el-dropdown class="user-name" trigger="click" @command="handleCommand">
            <span class="el-dropdown-link"> {{ username }} <i class="el-icon-caret-bottom"></i></span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item disabled>修改密码</el-dropdown-item>
              <el-dropdown-item command="loginout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>

    <!-- 页面左侧二级菜单栏，和主体内容区域部分 -->
    <el-main>
      <router-view></router-view>
    </el-main>

  </div>
</template>

<script>
import bus from "@/utils/bus";
import { removeToken } from '@/utils/auth';

export default {
   data(){
       return{
           globalTheme: false,
           menuList: []
       }
   },
   computed: {
       username(){
          return localStorage.getItem('ms_username') || '';
       },
       toIndex(){
          // 根据路径绑定到对应的一级菜单，防止页面刷新重新跳回第一个
          return '/' + this.$route.path.split('/')[1];
       },
    },
    created() {
        this.menuList = this.$store.getters.routes
        this.globalTheme = JSON.parse(localStorage.getItem('global_theme'));
        // 将 globalTheme 的值传给父组件
        bus.$emit('global_theme', this.globalTheme);
        this.initCurrentRoutes();
    } ,
    methods: {
        // 切换菜单栏
        handleSelect(path){
            // 获取当前路由下的子路由
            const route = this.menuList.find(item => item.path === path)
            console.log('route', route)
            if (route.children != null && route.children && route.children.length) {
                // 默认显示当前路由下的第一个子路由
                this.$router.push({
                    path: path + '/'+route.children[0].path
                });
            }else {
                this.$router.push({
                    path: path
                });
            }
            // 把选中路由的子路由保存到store
            this.$store.commit('SET_CURRENT_ROUTES', route)
        },
        // 用户名下拉菜单选择事件
        handleCommand(command){
            // 退出登录
            if(command === 'loginout'){
                localStorage.removeItem('ms_username');
                removeToken();
                location.reload()
                this.$router.push({
                    path: '/Login'
                });
            }
        },
        // 切换主题
        handleChangeStyle(){
            this.globalTheme = !this.globalTheme;
            // 本地存储选择的 globalTheme
            localStorage.setItem('global_theme', this.globalTheme);
            // 将 globalTheme 的值传给父组件
            bus.$emit('global_theme', this.globalTheme);
        },
        initCurrentRoutes() {
            // 通过当前路径找到二级菜单对应项，存到store，用来渲染左侧菜单
            const { path } = this.$route
            let route = this.menuList.find(
              item => item.path === '/' + path.split('/')[1]
            )
            // 如果找不到这个路由，说明是首页
            if (!route) {
              route = this.menuList.find(item => item.path === '/')
            }
            this.$store.commit('SET_CURRENT_ROUTES', route)
        },
    }
}
</script>

<style scoped>
.wrapper {
  width: 100%;
  height: 100%;
  background: #f0f0f0;
}
.header {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  height: 70px;
  font-size: 22px;
}
/*---文字样式---*/
.header .logo {
    float: left;
    margin-left: 60px;
    margin-top: 17.5px;
    height: 29px;
    width: 160px;
    vertical-align: middle;
}
/*---图片样式---*/
/*.header .logo {*/
/*  float: left;*/
/*  margin-left: 0px;*/
/*  margin-top: 0px;*/
/*  height: 100%;*/
/*  width: 250px;*/
/*  vertical-align: middle;*/
/*}*/
/* --------------- 用户头像区域的样式 ---------------- */
.header-right {
  float: right;
  padding-right: 50px;
}
.header-user-con {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 70px;
}
.user-avator {
  margin-left: 20px;
}
.user-avator img {
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.user-name {
  margin-left: 10px;
}
.el-dropdown-link {
  cursor: pointer;
}
.el-dropdown-menu__item {
  text-align: center;
}
/* --------------- 水平一级菜单栏的样式--------------------- */
.el-menu.el-menu--horizontal {
  border-bottom: none !important;
  float: left;
  margin-left: 50px;
  background: transparent;
}
.el-menu--horizontal > .el-menu-item.is-active {
  /* border-bottom: 2px solid #3989fa;
  color: #3989fa; */
  font-weight: bold;
}
.el-menu--horizontal > .el-menu-item {
  font-size: 16px;
  margin: 0 15px;
}
</style>
